package cn.yomigaeri.warn.config;

import cn.yomigaeri.common.entity.po.Rule;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CacheConfig
 * @Package: cn.yomigaeri.warn.config
 * @Description:
 * @Author Yomigaeri
 * @Create 2025/6/5 11:09
 * @Version 1.0
 */
@Configuration
public class CacheConfig {

    @Bean
    public CacheManager advancedCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.registerCustomCache("rules", Caffeine.newBuilder()
                .maximumSize(1024)
//                .expireAfterWrite(1, TimeUnit.HOURS)
                .build());
        return cacheManager;
    }

    @Bean
    public Cache<String, List<Rule>> ruleCache() {
        return Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(1000)
                .expireAfterWrite(1, TimeUnit.HOURS) // 1小时后过期
                .recordStats()
                .build();
    }
}